package com.hcr.elecident;


import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.fastjson.JSON;
import com.hcr.entity.H5PayEntity;
import com.hcr.entity.H5PayResult;
import com.hcr.util.MyConst;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class H5PayActivity extends AppCompatActivity {

    public Handler handler = new Handler();
    private String h5url = "file:///android_asset/h5pay.html";
    String referer = "http://110passport.com";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5_pay);

        final WebView mWebView = (WebView) findViewById(R.id.h5pay);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("weixin://wap/pay?")) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                    return true;
                }else if (url.startsWith("tel:")) {//H5打开电话
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    H5PayActivity.this.startActivity(intent);
                } else {
                    //H5微信支付要用，不然说"商家参数格式有误"
                    Map<String, String> extraHeaders = new HashMap<>();
                    extraHeaders.put("Referer", referer);
                    view.loadUrl(url, extraHeaders);
                    referer = url;
                    return true;
                }
                view.loadUrl(url);
                return true;
            }
        });


        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.loadUrl(h5url);
    }

    public void onReceivedSslError(WebView webview, SslErrorHandler sslErrorHandler,
                                   SslError sslError) {
    }
}
